var stockChannelDAO = require('../controller/controller.stockChannel');
// var stockEstimateDAO = require('../controller/controller.stockEstimate');
var async = require('async');

/**
 * 股票相关栏目Service
 */
module.exports = {
    getListByChannelType: function (req, res, next) {
        stockChannelDAO.getListByChannelType(req.body.params, function (err, result) {
            next(err, result.obj);
        });
    },
    update: function (req, res, next) {
        stockChannelDAO.update(req.body.params, function (err, result) {
            next(err, result.obj);
        });
    },
    getDetail: function (req, res, next) {
        var params = req.body.params;
        async.waterfall([
            function (cb) {
                stockChannelDAO.get({rowId:params.rowId}, function (err, result) {
                    if (err) {
                        cb(err, null);
                    } else {
                        cb(null, result.obj);
                    }
                });
            },
            // function (stockChannel, cb) {
            //     stockEstimateDAO.getByBeginAndEndDate({
            //         stockChannelId: stockChannel.rowId,
            //         beginMonth: params.beginMonth,
            //         endMonth: params.endMonth,
            //         beginYear: params.beginYear,
            //         endYear: params.endYear
            //     }, function (err, result) {
            //         if (err) {
            //             cb(err, null);
            //         } else {
            //             stockChannel.estimates = result.obj
            //             cb(null, stockChannel);
            //         }
            //     });
            // },
            ],function(err,res){
                if(err){
                    next(err,{code: 404, message: err})
                } else{
                    next(null,{code: 200, result:res})
                }
        });

    },
    deleteByIds: function (req, res, next) {
        var rowIds = req.body.rowIds || [];
        if (rowIds.length > 0) {
            stockChannelDAO.deleteByIds(rowIds, function (err, result) {
                next(err, result);
            });
        } else {
            next();
        }
    }
}